« How to Get a List of Subfolders of a Folder? »
Thu July-19-2007, 7:58:41 PM
A VB.Net Program I wrote to demonstrate how to get a list of subfolders of a folder. There are two different methods used to accomplish this. One is the built-in DirectoryInfo Object GetDirectories method, the other is a recursive function call.

You can download the whole source code.

Here is seom code from the program:

 'Recursive Function To Find All SubDirectory Names
    Public Sub SubFoldersRecursive(ByVal Root As DirectoryInfo, ByRef Status As String)
        Try
            Status += Root.FullName + vbNewLine
            If (Root.GetDirectories.Length <= 0) Then
                Exit Sub
            End If
            Dim SD As DirectoryInfo
            For Each SD In Root.GetDirectories
                SubFoldersRecursive(SD, Status)
            Next
        Catch ex As Exception
            Status += ex.Message + vbNewLine
        End Try
    End Sub
    'NON-Recursive Function To Find All SubDirectory Names
    Public Sub SubFolders(ByVal Root As DirectoryInfo, ByRef Status As String)
        Dim Dir As DirectoryInfo
        Status += Root.FullName + vbNewLine
        For Each Dir In Root.GetDirectories("*", SearchOption.AllDirectories)
            Status += Dir.FullName + vbNewLine
        Next
    End Sub